import 'package:flutter/material.dart';
import 'dart:math' as math;
class DashBoard extends StatelessWidget {
  final double radius;
  final double speed;
  final TextStyle speedStyle;
  final String unit;
  final Color tickColor;
  final double lineLength;
  final Color valueColor;

  const DashBoard(
      {Key key,
      this.radius,
      this.speed,
      this.unit,
      this.tickColor = Colors.white,
      this.speedStyle = const TextStyle(
          color: Colors.white, fontWeight: FontWeight.w800, fontSize: 30),
          this.lineLength = 45,this.valueColor = Colors.green})
      : assert(radius != null),
        assert(speed != null),
        assert(unit != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      child: Container(
          height: radius*2,
          width: radius*2,
          child: Center(
              child: Text(
            "${speed.toString()}\r\n${unit}",
            style: speedStyle,
          ))),
      painter: BoardPainter(tickColor,radius,lineLength,valueColor),
    );
  }
}

class BoardPainter extends CustomPainter {
  final Color tickColor;
  final double radius;
  final double lineLength;
  final Color valueColor;
  BoardPainter(this.tickColor,this.radius,this.lineLength,this.valueColor);

  @override
  void paint(Canvas canvas, Size size) {
    Offset center = Offset(radius,radius);
    double tickLength = 20;
    int deg = 360;
    double rdeg = 2*math.pi;
    Paint paint = Paint();
    paint.style = PaintingStyle.stroke;
    paint.color = tickColor;
    paint.strokeWidth=4;
    paint.strokeCap= StrokeCap.round;
    paint.colorFilter = ColorFilter.srgbToLinearGamma();

    Paint  paintValue = Paint();
    paintValue.style = PaintingStyle.stroke;
    paintValue.color = valueColor;
    paintValue.strokeWidth=4;
    paintValue.strokeCap= StrokeCap.round;
    paintValue.colorFilter = ColorFilter.srgbToLinearGamma();

    //canvas.drawArc(Rect.fromCenter(center:center,width: size.width,height: size.height),
    //    3.14/4*3,3.14+3.14/4*2,false,paint);
    for(int i =0;i<=36;i++){
      if(deg>=45 && deg<315){
        double x =  math.sin(rdeg)*radius+size.width/2.0;
        double y = math.cos(rdeg)*radius+size.height/2.0;

        double x1 = math.sin(rdeg)*this.lineLength/2+x;
        double y1 = math.cos(rdeg)*this.lineLength/2+y;
        Offset point1 = Offset(x1,y1);
        Offset point2 = Offset(x,y);
        canvas.drawLine(point1, point2, paint);
        if(deg<=180){
          canvas.drawLine(point1, point2, paintValue);
        }
      }

      deg = deg-10;
      rdeg = (math.pi/180)*deg;
    }

  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
